export default class Question {
  id:number;
  title:string;
  answer:string;
  proficiency:number;
  errorFlag:boolean;
  bakAnswer:[]
  constructor(title:string,answer:string,type:string) {
    this.title=title;
    this.answer=answer;
    if (type&&type!='api'){
      this.loadFromCache()
    }
  }
  //保存熟练度和答错标志到缓存
  saveToCache() {
    let obj={proficiency:this.proficiency,
    errorFlag:this.errorFlag}
    localStorage.setItem(this.title,JSON.stringify(obj))
  }
  //从缓存中取出熟练度和答错标志
  loadFromCache(){
    let that=this
    console.log('loadFromCache')
    localStorage.getItem(this.title)
      .then(item=>{
        if (item){
          let obj=JSON.parse(item)
          that.proficiency=obj.proficiency;
          that.errorFlag=obj.errorFlag;
        }else{
          that.proficiency=0;
          that.errorFlag=false;
        }
      })

  }
  //答错了
  degrade(){
    this.proficiency=0
    this.errorFlag=true
    this.saveToCache()
  }
  //提升熟练度
  upgrade(){
    if (this.proficiency>=1){
      this.errorFlag=false
      return;
    }
    this.proficiency+=0.2;
    this.saveToCache()
  }
}
